#include "header.h"

int main(void)
{
	int i, j;
	float quick_start, quick_end;
	float quick2_start, quick2_end;
	float quick4_start, quick4_end;
	float quick8_start, quick8_end;
	float quick16_start, quick16_end;
	float merge_start, merge_end;
	float merge2_start, merge2_end;
	float merge4_start, merge4_end;
	float merge8_start, merge8_end;
	float merge16_start, merge16_end;

	timing times;	//For total time of each algorithm's runtime
	memset(&times,0,sizeof(timing));	//Initialize all values within the structure timing.

	if(readArray(quick_single_thread)!=1000)
		printf("Problem with reading!\n");

//****Part 1 - Single Thread Quicksort*****************************************
	quick_start = getTime();
	for(i=0;i<1000;i++)
		quick_sort(quick_single_thread,0,999,i);
	quick_end = getTime();
	times.quick_time += quick_end-quick_start;

	//Check Single Thread Quicksort
/*	if(check_sort_rows(quick_single_thread)!=0)
		printf("fault!\n");
	else
		printf("errorless!\n");*/

//****Part 2 - Multi Thread Quicksort*****************************************
	//Dual Thread
	quick2_start = getTime();
	run_quicksort_multithread(2);
	quick2_end = getTime();
	times.quick2_time += quick2_end-quick2_start;
/*	if(check_sort_rows(quick_multi_thread2)!=0)
		printf("fault! %d\n",check_sort_rows(quick_multi_thread2));
	else
		printf("errorless!\n");*/

	//Quad Thread
	quick4_start = getTime();
	run_quicksort_multithread(4);
	quick4_end = getTime();
	times.quick4_time += quick4_end-quick4_start;
/*	if(check_sort_rows(quick_multi_thread4)!=0)
		printf("fault! %d\n",check_sort_rows(quick_multi_thread4));
	else
		printf("errorless!\n");*/

	//Octa Thread
	quick8_start = getTime();
	run_quicksort_multithread(8);
	quick8_end = getTime();
	times.quick8_time += quick8_end-quick8_start;
/*	if(check_sort_rows(quick_multi_thread8)!=0)
		printf("fault! %d\n",check_sort_rows(quick_multi_thread8));
	else
		printf("errorless!\n");*/

	//16 Thread
	quick16_start = getTime();
	run_quicksort_multithread(16);
	quick16_end = getTime();
	times.quick16_time += quick16_end-quick16_start;
/*	if(check_sort_rows(quick_multi_thread16)!=0)
		printf("fault! %d\n",check_sort_rows(quick_multi_thread16));
	else
		printf("errorless!\n");*/

//****Part 3 - Single Thread Merge********************************************
	merge_start = getTime();
	arraytable = merge(quick_single_thread, 0,1023, 1024);	//arraytable[1022] is the final array sized 1,000,000
	merge_end = getTime();
	times.merge_time += merge_end-merge_start;
/*	if(check_sort_row(arraytable[1022])!=0)
		fprintf(stderr,"Check Error!\n");
	else
		fprintf(stderr,"Check Correct!\n");*/

//****Part 4 - Multi Thread Merge********************************************
	//2 Thread
	merge2 = (int**)malloc(sizeof(int*)*1024);
	merge2_start = getTime();
	run_mergesort_multithread(2);
	//fprintf(stderr,"merge2[1022][999999] = %d\n",arraytable2[1022][999999]);
	merge2_end = getTime();
	times.merge2_time += merge2_end-merge2_start;

	//4 Thread
	merge4 = (int**)malloc(sizeof(int*)*1024);
	merge4_start = getTime();
	run_mergesort_multithread(4);
	merge4_end = getTime();
	times.merge4_time += merge4_end-merge4_start;

	//8 Thread
	merge8 = (int**)malloc(sizeof(int*)*1024);
	merge8_start = getTime();
	run_mergesort_multithread(8);
	merge8_end = getTime();
	times.merge8_time += merge8_end-merge8_start;

	//16 Thread
	merge16 = (int**)malloc(sizeof(int*)*1024);
	merge16_start = getTime();
	run_mergesort_multithread(16);
	merge16_end = getTime();
	times.merge16_time += merge16_end-merge16_start;
	/*if(check_sort_row(c)!=0)
				printf("fault! %d\n",check_sort_row(c));
			else
				printf("errorless!\n");*/

	print_result(&times);
	print_to_file(&times);

	free_manager();
	return 0;
}
